package dyyx;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

public class LogProxy implements MethodInterceptor {

	private Enhancer enhancer = new Enhancer();

	public Object getObject(Class clz) {
		enhancer.setSuperclass(clz);
		enhancer.setCallback(this);
		return enhancer.create();
	}

	public Object intercept(Object o, Method method, Object[] args, MethodProxy proxy) throws Throwable {
		System.out.println("log before method run," + method);
		Object result = proxy.invokeSuper(o, args);
		return result;
	}

	public static void main(String[] args) {
		LogProxy logProxy = new LogProxy();
		Hello hello = (Hello) logProxy.getObject(Hello.class);
		hello.sayHello("tiger");
	}
}
